import { query as queryUsers, queryCurrent ,queryCurrentMenu, updateStateVideo} from '../services/user';
import { message } from 'antd';

export default {
  namespace: 'user',

  state: {
    list: [],
    currentUser: {},
    menuForCurrentUser:[]
  },

  effects: {
    *fetch(_, { call, put }) {
      const response = yield call(queryUsers);
      yield put({
        type: 'save',
        payload: response,
      });
    },

    *updateStateVideo({ payload }, { call, put }) {
      const response = yield call(updateStateVideo, payload);
      yield put({
        type: 'save',
        payload: {
          data: response.data
        },
      });
    },

    *fetchCurrent(_, { call, put }, callback) {
      const response = yield call(queryCurrentMenu);//queryCurrent
      // console.log("responseresponse")
      // console.log(response)
      yield put({
        type: 'saveCurrentUser',
        payload: {
          menuForCurrentUser: [],
        },
      });
      if(response.result.code != 200){
        message.error(response.result.message);       
      } 
        
      yield put({
        type: 'saveCurrentUser',
        payload: {
          menuForCurrentUser: response.data,
        },
      }); 
      if(callback)callback(response.result.code);
    },
    // *fetchMenu(_, { call, put }) {
    //   const response = yield call(queryCurrentMenu);//
    //   // console.log("AAAAAAAAAresponseAA")
    //   // console.log(response)
    //   yield put({
    //     type: 'saveCurrentUser',
    //     payload: {
    //       menuForCurrentUser: response.data,
    //     },
    //   });
    // },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    saveCurrentUser(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    changeNotifyCount(state, action) {
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
          notifyCount: action.payload,
        },
        menuForCurrentUser: {
          ...state.menuForCurrentUser,
          notifyCount: action.payload,
        },
      };
    },
  },
};
